package mc.web;

import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import mc.bus.BazaLekarzy;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 * Kontroler wyswietlajacy liste lekarzy  
 * @author nickname
 *
 */

public class ShowLekarzList extends AbstractController {

	private BazaLekarzy bazalekarzy;
	private final Log logger = LogFactory.getLog(getClass());	
	private String firstView;
	private String redirectAfterDelete;
	

	@Override
	public ModelAndView handleRequestInternal (HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		
		if(request.getParameter("action") != null){
			
			if(request.getParameter("action").equals("delete")){
						
				int id = new Integer(request.getParameter("id"));
				logger.info("Usuwam lekarza o id="+id);
				bazalekarzy.deleteLekarz(id);
			
				response.sendRedirect(getRedirectAfterDelete());
			}
		}
		
	
		String now = (new java.util.Date().toString());
		HashMap <Object,Object> myModel = new HashMap<Object, Object>();
		myModel.put("lekarze", bazalekarzy.getAllLekarze());
		myModel.put("czas", now);
		
		return new ModelAndView(getFirstView(), "model", myModel);
	}


	
	public BazaLekarzy getBazalekarzy() {
		return bazalekarzy;
	}

	public void setBazalekarzy(BazaLekarzy bazalekarzy) {
		this.bazalekarzy = bazalekarzy;
	}

	public String getFirstView() {
		return firstView;
	}

	public void setFirstView(String firstView) {
		this.firstView = firstView;
	}

	public String getRedirectAfterDelete() {
		return redirectAfterDelete;
	}

	public void setRedirectAfterDelete(String redirectAfterDelete) {
		this.redirectAfterDelete = redirectAfterDelete;
	}


}
